package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.ClassQaAnswer;
import com.hs.service.ClassQaAnswerService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("classqaanswer")
public class ClassQaAnswerController {
    @Autowired
    private ClassQaAnswerService classQaAnswerService;
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        int i = classQaAnswerService.deleteById(id);
        if (i > 0) {
            return R.ok("操作成功");
        }else{
            return R.fail("操作失败");
        }
    }
    @PostMapping("add")
    public R addclassqaanswer(@RequestBody ClassQaAnswer classQaAnswer) {
        int i = classQaAnswerService.addClassQaAnswer(classQaAnswer);
        if (i > 0) {
            return R.ok("操作成功");
        }else{
            return R.fail("操作失败");
        }
    }
    @PutMapping("update")
    public R updateclassqaanswer(@RequestBody ClassQaAnswer classQaAnswer) {
        int i = classQaAnswerService.updateClassQaAnswer(classQaAnswer);
        if (i > 0) {
            return R.ok("操作成功");
        }else{
            return R.fail("操作失败");
        }
    }
    @GetMapping("{id}")
    public R getclassqaanswerByid(@PathVariable("id") Integer id) {
        ClassQaAnswer cc = classQaAnswerService.getClassQaAnswerById(id);
        return R.ok(cc);
    }
    @GetMapping("page")
    public  R getclassqaansweBypage(@RequestParam(defaultValue = "1",required = false)int pageNum,
                                    @RequestParam(defaultValue = "2",required = false)int pageSize){
        PageInfo<ClassQaAnswer> cc = classQaAnswerService.getClassQaAnswerPage(pageNum, pageSize);
        return R.ok(cc);
    }
    @GetMapping("getAnwserByCqqId")
    public R getAnwserByCqqId(int cqqId){
        List<ClassQaAnswer> classQaAnswerByQaId = classQaAnswerService.getClassQaAnswerByQaId(cqqId);
        // 创建一个Map，明确指定"list"作为键
        Map<String, Object> result = new HashMap<>();
        result.put("list", classQaAnswerByQaId);
        return R.ok(result);
    }
}
